#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <pthread.h>
#include "uart_tool.h"

int fd;

void *readThread(void *argv){
	int x;
	while(1){

		if((x = mySerialGetChar(fd)) != -1){
			switch(x){
				case 'N':printf("下一条\n");
						 //printf("out:%c\n",x);
						 system("adb shell input swipe 500 1500 500 500");
						 break;
				case 'G':printf("点赞\n");
						 //printf("out:%c\n",x);
						 system("adb shell \"input tap 500 1000; input tap 500 1000\"");
						 break;
				case 'L':printf("上一条\n");
						 //printf("out:%c\n",x);
						 system("adb shell input swipe 500 500 500 1500");
						 break;
				case 'O':printf("回到主屏幕\n");
						 //printf("out:%c\n",x);
						 system("adb shell input keyevent 3");
						 break;
				default:printf("miss");
						break;
			}
		}
	}
	return NULL;
}

int main(int argc,char **argv){
	char deviceName[32] = {'\0'};
	pthread_t pid_read;

	if(argc < 2){
		perror("/dev/ttyS?");
		exit(-1);
	}

	strcpy(deviceName,argv[1]);

	if((fd =  mySerialOpen(deviceName, 115200)) < 0){
		perror("serialOpen");
		exit(-1);
	}

	pthread_create(&pid_read, NULL, readThread, NULL);

	pthread_join(pid_read,NULL);

	return 0;
}
